package com.lishu.carsafe.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.ContactDetailModel;
import com.lishu.carsafe.utils.ToastUtil;

public class ContactDetailActivity extends BaseActivity implements View.OnClickListener{
    //private ImageView avatar;
    private TextView name, unit, phone, post, remark;
    private LinearLayout phone_layout;
    private String phoneNum;
    private String contactId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);

        initView();

        contactId = (String) getIntent().getStringExtra("contactId");
        getContactDetail(contactId);
        testData();
    }

    private void initView() {
        setTopTitle("联系人详情");

        //avatar = findViewById(R.id.avatar);
        name = findViewById(R.id.name);
        unit = findViewById(R.id.unit);
        phone = findViewById(R.id.phone);
        post = findViewById(R.id.post);
        remark = findViewById(R.id.remark);
        phone_layout = findViewById(R.id.phone_layout);
    }


    private void getContactDetail(String contactId) {
        HttpLoader.getContactDetail(contactId, new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }

                ContactDetailModel.ContactDetailBean contactDetailBean = ((ContactDetailModel) model).getData();
                setData(contactDetailBean);
            }
        });
    }


    private void testData() {
        ContactDetailModel.ContactDetailBean contactEntity = new ContactDetailModel().new ContactDetailBean();
        contactEntity.setName("测试");
        contactEntity.setNumber("x-1001-y");
        contactEntity.setDriverCardNo("X58888");
        contactEntity.setAddress("13766668888");
        contactEntity.setConcat("南京市江宁区");
        setData(contactEntity);
    }

    private void setData(ContactDetailModel.ContactDetailBean contactEntity) {
        if (contactEntity != null) {
            name.setText(contactEntity.getName());
            unit.setText(contactEntity.getNumber());
            post.setText(contactEntity.getDriverCardNo());
            remark.setText(contactEntity.getAddress());

            phoneNum = contactEntity.getConcat();
            if(!TextUtils.isEmpty(phoneNum)) {
                phone.setText(phoneNum);
                phone_layout.setOnClickListener(this);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.phone_layout:
                checkPermissions(new String[]{Manifest.permission.CALL_PHONE}, 100);
                break;
        }
    }

    protected void onRequestPermissionsGranted(int requestCode) {
        if (requestCode == 100) {
            try {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
                startActivity(intent);
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
    }
}
